package com.terabits.smartriver.netty.config;

import cn.hutool.core.convert.Convert;
import com.terabits.smartriver.mapper.RegionMapper;
import com.terabits.smartriver.service.CalculationService;
import com.terabits.smartriver.util.CallBackMessage;
import com.terabits.smartriver.util.DataReduction;
import com.terabits.smartriver.util.DataValida;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.springframework.data.mongodb.core.aggregation.ConvertOperators;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.terabits.smartriver.util.DateUtils.DateToString;

@Component
@ChannelHandler.Sharable
public class ServerHandler extends ChannelInboundHandlerAdapter {
    @Resource
    private CalculationService calculationService;

    @Resource
    private RegionMapper regionMapper;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        System.out.println("******************************开始*********************************");

        System.out.println("===============");
        System.out.println(msg);
        System.out.println(DateToString(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
        System.out.println("===============");

        //就是盒子IMEI
        String channelID = null;

        String data = (String) msg;
        if (DataValida.ValidateHeadAndFeet(data)) {
            int packageType = DataValida.getPackageType(data);
            if (packageType == -1) {
                return;
            }
            //去除 头 和 尾 就是IMEI开头
            data = DataReduction.ResctionHeadAndFeet(data);
            //进行数据校验 现在是不做校验 直接返回true
            if (DataValida.ValidateSUMCode(DataReduction.ResctionData(data), DataReduction.ResctionCRCCode(data))) {
                //返回通信内容，除去校验码
                //                    data = DataReduction.ResctionData(data);

                //返回设备唯一ID  盒子IMEI号
                channelID = DataReduction.ResctionID(data);
                String boxNum = getBoxNum(channelID);
                String interfaces = "," + regionMapper.selectStrSQL("SELECT GROUP_CONCAT(box_interface) i FROM device WHERE box_num='" + boxNum + "'") + ",";

                //去掉IMEI
                data = DataReduction.ResctionDataNoID(data);
                //时间
                String time = DataReduction.ResctionTime(data);

                String RealData = DataReduction.ResctionRealData(data);

                switch (packageType) {
                    case 0:
                        dataProcessingType0(ctx, RealData, boxNum, interfaces);
                        break;
                    case 1:
                        dataProcessingType1(ctx, RealData, boxNum, interfaces);
                        break;
                    case 2:
                        dataProcessingType2(ctx, RealData, boxNum, interfaces);
                        break;
                    case 3:
                        dataProcessingType3(ctx, RealData, boxNum, interfaces);
                        break;
                    case 4:
                        dataProcessingType4(ctx, RealData, boxNum, interfaces);
                        break;
                    case 5:
                        dataProcessingType5(ctx, RealData, boxNum, interfaces);
                        break;
                    default:
                        return;
                }

            } else {
                ctx.writeAndFlush(CallBackMessage.ERROR.duplicate());
                // ctx.close();
            }
        } else {
            ctx.writeAndFlush(CallBackMessage.ERROR.duplicate());
            //  ctx.close();
        }

        ByteBuf out = getByteBuf(ctx);
        ctx.channel().writeAndFlush(out);
        System.out.println("******************************结束*********************************");
    }

    /**
     * 绿维报文格式0
     *
     * @param ctx
     * @param realData 报文数据
     * @param boxNum   盒子的IMEI
     */
    private void dataProcessingType0(ChannelHandlerContext ctx, String realData, String boxNum, String interfaces) {
        if (boxNum != null) {
            Map<Integer, Double> digital = new HashMap<Integer, Double>();
            Map<Integer, String> digitalClose = new HashMap<Integer, String>();
            Map<Integer, String> digitalBreak = new HashMap<Integer, String>();
            Date currentDate = new Date();
            String createTime = DateToString(currentDate, null);

            //STA:572LWHJ20191119C;L:0306;TM:190614162701;C:36;
            // A01:0.022;
            // P01:000000005;P02:000000005;P03:000000005;P04:000000005;P05:000000005;P06:000000005;P07:000000005;P08:000000005;
            // P51:000000082;P52:000000082;P53:000000082;P54:000000082;P55:000000082;P56:000000082;P57:000000082;P58:000000083;
            // K01:00000000;3E


            //realData A09:00000;A10:00000;A11:00000;A12:00000;
            // K01:00000101010030000000101010103111111110000000;
            // P01:000012345;P02:000012345;P03:000012345;P04:000012345;P05:000012345;P06:000012345;P07:000012345;P08:000012345;P09:000012345;P10:000012345;P11:000012345;P12:000012345;
            // P51:000012345;P52:000012345;P53:000012345;P54:000012345;P55:000012345;P56:000012345;P57:000012345;P58:000012345;P59:000012345;P60:000012345;P61:000012345;P62:000012345;

            String[] datas = realData.split(";");

            for (String dataOne : datas) {
                String[] interfaceAndData = dataOne.split(":");
                if (interfaceAndData.length != 2) {
                    break;
                }
                //如果是P01-P12 代表 数字量闭合时长
                //00000  0000
                if ("P0".equals(interfaceAndData[0].substring(0, 2)) || "P1".equals(interfaceAndData[0].substring(0, 2))) {
                    int i = Integer.parseInt(interfaceAndData[0].substring(1).replaceAll("^(0+)", ""));
                    String countTime = getCountTime(interfaceAndData);
                    digitalClose.put(i, countTime);
                }
                //如果是P51-P60 代表 数字量断开时长
                else if ("P5".equals(interfaceAndData[0].substring(0, 2)) || "P6".equals(interfaceAndData[0].substring(0, 2))) {
                    int i = Integer.parseInt(interfaceAndData[0].substring(1).replaceAll("^(0+)", ""));
                    String countTime = getCountTime(interfaceAndData);
                    digitalBreak.put(i - 50, countTime);
                }
                //如果是K01 代表 开关量状态 就要前12个
                else if ("K01".equals(interfaceAndData[0])) {
                    //K01:00000101010030000000000000000000000000000111
                    String digitalAll = interfaceAndData[1];
                    for (int i = 0; i < digitalAll.length(); i++) {
                        String value = digitalAll.substring(i, i + 1);
                        digital.put(i + 1, Double.parseDouble(value));
                    }
                    //                    //后13到44个  位置就是12 43
                    //                    for (int i = 12; i < 44; i++) {
                    //                        calculationService.reportProcessing(boxNum, "K" + (i+1), Double.parseDouble(digitalAll.substring(i, i + 1)), null, null, createTime);
                    //                    }
                } else if ("A".equals(interfaceAndData[0].substring(0, 1))) {
                    if (interfaces.contains("," + interfaceAndData[0] + ",")) {
                        Double data = Double.parseDouble(interfaceAndData[1]);
                        calculationService.reportProcessing(boxNum, interfaceAndData[0], data, null, null, createTime);
                    }
                } else {
                    //不做处理
                }
            }

            for (Map.Entry<Integer, Double> entry : digital.entrySet()) {
                String boxInterface = "K" + String.format("%02d", entry.getKey());
                if (interfaces.contains("," + boxInterface + ",")) {
                    String closeTime = digitalClose.get(entry.getKey());
                    String breakTime = digitalBreak.get(entry.getKey());
                    if (StringUtils.isEmpty(closeTime)) {
                        closeTime = "0";
                    }
                    if (StringUtils.isEmpty(breakTime)) {
                        breakTime = "0";
                    }
                    calculationService.reportProcessing(boxNum, boxInterface, entry.getValue(), closeTime, breakTime, createTime);
                }
            }

        }
    }

    /**
     * 绿维报文格式1
     *
     * @param ctx
     * @param realData 报文数据
     * @param boxNum   盒子的IMEI
     */
    private void dataProcessingType1(ChannelHandlerContext ctx, String realData, String boxNum, String interfaces) {
        if (boxNum != null) {
            Map<Integer, Double> digital = new HashMap<Integer, Double>();
            Map<Integer, String> digitalClose = new HashMap<Integer, String>();
            Map<Integer, String> digitalBreak = new HashMap<Integer, String>();
            Date currentDate = new Date();
            String createTime = DateToString(currentDate, null);
            //#STA:5720002202002221;L:0306;TM:200224095200;C:36;
            // A01:0.699;A02:00000;
            // P01:000016500;P02:000000000;P03:000000002;P04:000000000;P05:000016500;P06:000000000;P07:000001235;P08:000000000;
            // P51:000000008;P52:000000000;P53:000000001;P54:000000000;P55:000000008;P56:000000000;P57:000000003;P58:000000000;
            // K01:10001000;77


            //#STA:572YJBS20170417B;L:0306;TM:190614162701;C:36;
            // A01:00000;A02:00000;
            // P01:000000005;P02:000000005;P03:000000005;P04:000000005;P05:000000005;P06:000000005;P07:000000005;P08:000000005;
            // P51:000000082;P52:000000082;P53:000000082;P54:000000082;P55:000000082;P56:000000082;P57:000000082;P58:000000083;
            // K01:00000000;3E


            //realData A09:00000;A10:00000;A11:00000;A12:00000;
            // K01:00000101010030000000101010103111111110000000;
            // P01:000012345;P02:000012345;P03:000012345;P04:000012345;P05:000012345;P06:000012345;P07:000012345;P08:000012345;P09:000012345;P10:000012345;P11:000012345;P12:000012345;
            // P51:000012345;P52:000012345;P53:000012345;P54:000012345;P55:000012345;P56:000012345;P57:000012345;P58:000012345;P59:000012345;P60:000012345;P61:000012345;P62:000012345;

            String[] datas = realData.split(";");

            for (String dataOne : datas) {
                String[] interfaceAndData = dataOne.split(":");
                if (interfaceAndData.length != 2) {
                    break;
                }
                //如果是P01-P12 代表 数字量闭合时长
                //00000  0000
                //#STA:5720002202002221;L:0306;TM:200224164900;C:41;A01:0.693;A02:00000;
                // P01:000034720;P02:000006801;P03:000000002;P04:000000000;P05:000034720;P06:000006801;P07:000009719;P08:000000000;
                // P51:000000008;P52:000000001;P53:000000001;P54:000000000;P55:000000008;P56:000000001;P57:000000011;P58:000000000;
                // K01:01000110;A8
                if ("P0".equals(interfaceAndData[0].substring(0, 2)) || "P1".equals(interfaceAndData[0].substring(0, 2))) {
                    int i = Integer.parseInt(interfaceAndData[0].substring(1).replaceAll("^(0+)", ""));
                    String countTime = getCountTime(interfaceAndData);
                    digitalClose.put(i, countTime);
                }
                //如果是P51-P60 代表 数字量断开时长
                else if ("P5".equals(interfaceAndData[0].substring(0, 2)) || "P6".equals(interfaceAndData[0].substring(0, 2))) {
                    int i = Integer.parseInt(interfaceAndData[0].substring(1).replaceAll("^(0+)", ""));
                    String countTime = getCountTime(interfaceAndData);
                    digitalBreak.put(i - 50, countTime);
                }
                //如果是K01 代表 开关量状态 就要前12个
                else if ("K01".equals(interfaceAndData[0])) {
                    //K01:00000101010030000000000000000000000000000111
                    String digitalAll = interfaceAndData[1];
                    for (int i = 0; i < digitalAll.length(); i++) {
                        String value = digitalAll.substring(i, i + 1);
                        digital.put(i + 1, Double.parseDouble(value));
                    }
                    //                    //后13到44个  位置就是12 43
                    //                    for (int i = 12; i < 44; i++) {
                    //                        calculationService.reportProcessing(boxNum, "K" + (i+1), Double.parseDouble(digitalAll.substring(i, i + 1)), null, null, createTime);
                    //                    }
                } else if ("A".equals(interfaceAndData[0].substring(0, 1))) {
                    if (interfaces.contains("," + interfaceAndData[0] + ",")) {
                        Double data = Double.parseDouble(interfaceAndData[1]);
                        calculationService.reportProcessing(boxNum, interfaceAndData[0], data, null, null, createTime);
                    }
                } else {
                    //不做处理
                }
            }
            for (Map.Entry<Integer, Double> entry : digital.entrySet()) {
                String boxInterface = "K" + String.format("%02d", entry.getKey());
                if (interfaces.contains("," + boxInterface + ",")) {
                    String closeTime = digitalClose.get(entry.getKey());
                    String breakTime = digitalBreak.get(entry.getKey());
                    if (StringUtils.isEmpty(closeTime)) {
                        closeTime = "0";
                    }
                    if (StringUtils.isEmpty(breakTime)) {
                        breakTime = "0";
                    }
                    String data = null;
                    System.out.println(data);
                    if (entry.getValue() == 1.0) {
                        data = "开启";
                    } else if (entry.getValue() == 0.0) {
                        data = "关闭";
                    }
                    calculationService.reportProcessing(boxNum, boxInterface, data, closeTime, breakTime, createTime);
                }
            }

        }
    }

//    public static void main(String[] args) {
//        String boxInterface = "K" + String.format("%02d", "K01");
//        System.out.println(boxInterface);
//    }

    /**
     * 绿维报文格式2
     *
     * @param ctx
     * @param realData 报文数据
     * @param boxNum   盒子的IMEI
     */
    private void dataProcessingType2(ChannelHandlerContext ctx, String realData, String boxNum, String interfaces) {
        if (boxNum != null) {
            Map<Integer, Double> digital = new HashMap<Integer, Double>();
            Map<Integer, String> digitalClose = new HashMap<Integer, String>();
            Map<Integer, String> digitalBreak = new HashMap<Integer, String>();
            Date currentDate = new Date();
            String createTime = DateToString(currentDate, null);


            //realData A01:00000;A02:00000;A03:00000;A04:00000;A05:00000;A06:00000;A07:00000;A08:00000;A09:00000;A10:00000;
            // K01:00000101010030000000101010103111111110000000;
            // P13:000012345;P14:000012345;P15:000012345;P16:000012345;P17:000012345;P18:000012345;P19:000012345;一直到P44:000012345;
            // P63:000012345;P64:000012345;P65:000012345;P66:000012345;P67:000012345;P68:000012345;P69:000012345;一直到P94:000012345;

            String[] datas = realData.split(";");

            for (String dataOne : datas) {
                String[] interfaceAndData = dataOne.split(":");
                if (interfaceAndData.length != 2) {
                    break;
                }
                int breakOrClose = Integer.parseInt(interfaceAndData[0].substring(1));
                //如果是P13-P44 代表 数字量闭合时长
                if (breakOrClose >= 13 && breakOrClose <= 44) {
                    int i = Integer.parseInt(interfaceAndData[0].substring(1).replaceAll("^(0+)", ""));
                    String countTime = getCountTime(interfaceAndData);
                    digitalClose.put(i - 12, countTime);
                }
                //如果是P63-P94 代表 数字量断开时长
                else if (breakOrClose >= 63 && breakOrClose <= 94) {
                    int i = Integer.parseInt(interfaceAndData[0].substring(1).replaceAll("^(0+)", ""));
                    String countTime = getCountTime(interfaceAndData);
                    digitalBreak.put(i - 62, countTime);
                }
                //如果是K01 代表 开关量状态 就要后32个  位置12 43
                else if ("K01".equals(interfaceAndData[0])) {
                    //K01:00000101010030000000000000000000000000000111
                    String digitalAll = interfaceAndData[1];
                    for (int i = 12; i < 44; i++) {
                        String value = digitalAll.substring(i, i + 1);
                        digital.put(i + 1, Double.parseDouble(value));
                    }

                    //                    //前12个  位置就是0 11
                    //                    for (int i = 0; i < 12; i++) {
                    //                        calculationService.reportProcessing(boxNum, "K" + String.format("%02d", (i + 1)), Double.parseDouble(digitalAll.substring(i, i + 1)), null, null, createTime);
                    //                    }
                } else {
                    //不做处理
                }
            }

            for (Map.Entry<Integer, Double> entry : digital.entrySet()) {
                String boxInterface = "K" + String.format("%02d", entry.getKey());
                if (interfaces.contains("," + boxInterface + ",")) {
                    String closeTime = digitalClose.get(entry.getKey());
                    String breakTime = digitalBreak.get(entry.getKey());
                    if (StringUtils.isEmpty(closeTime)) {
                        closeTime = "0";
                    }
                    if (StringUtils.isEmpty(breakTime)) {
                        breakTime = "0";
                    }
                    calculationService.reportProcessing(boxNum, boxInterface, entry.getValue(), closeTime, breakTime, createTime);
                }
            }

        }
    }

    /**
     * @param ctx
     * @param realData 报文数据
     * @param boxNum   盒子的IMEI
     */
    private void dataProcessingType3(ChannelHandlerContext ctx, String realData, String boxNum, String interfaces) {
        if (boxNum != null) {
            Map<Integer, Double> digital = new HashMap<Integer, Double>();
            Map<Integer, String> digitalClose = new HashMap<Integer, String>();
            Map<Integer, String> digitalBreak = new HashMap<Integer, String>();
            Date currentDate = new Date();
            String createTime = DateToString(currentDate, null);

            //realData A01:00000;A02:00000;A03:00000;A04:00000;A05:00000;A06:00000;A07:00000;A08:00000;A09:00000;A10:00000;
            // K01:00000101010000000000;
            // P01:000012345;P02:000012345;P03:000012345;P04:000012345;P05:000012345;P06:000012345;P07:000012345;P08:000012345;P09:000012345;P10:000012345;
            // P51:000012345;P52:000012345;P53:000012345;P54:000012345;P55:000012345;P56:000012345;P57:000012345;P58:000012345;P59:000012345;P60:000012345;

            String[] datas = realData.split(";");

            for (String dataOne : datas) {
                String[] interfaceAndData = dataOne.split(":");
                if (interfaceAndData.length != 2) {
                    break;
                }
                //如果是P01-P10 代表 数字量闭合时长
                if ("P0".equals(interfaceAndData[0].substring(0, 2)) || "P1".equals(interfaceAndData[0].substring(0, 2))) {
                    int i = Integer.parseInt(interfaceAndData[0].substring(1).replaceAll("^(0+)", ""));
                    String countTime = getCountTime(interfaceAndData);
                    digitalClose.put(i, countTime);
                }
                //如果是P51-P60 代表 数字量断开时长
                else if ("P5".equals(interfaceAndData[0].substring(0, 2)) || "P6".equals(interfaceAndData[0].substring(0, 2))) {
                    int i = Integer.parseInt(interfaceAndData[0].substring(1).replaceAll("^(0+)", ""));
                    String countTime = getCountTime(interfaceAndData);
                    digitalBreak.put(i - 50, countTime);
                }
                //如果是K01 代表 开关量状态 就要前10个
                else if ("K01".equals(interfaceAndData[0])) {
                    //K01:00000101010000000000
                    String digitalAll = interfaceAndData[1];
                    for (int i = 0; i < 10; i++) {
                        String value = digitalAll.substring(i, i + 1);
                        digital.put(i + 1, Double.parseDouble(value));
                    }
                } else if ("A".equals(interfaceAndData[0].substring(0, 1))) {
                    if (interfaces.contains("," + interfaceAndData[0] + ",")) {
                        Double data = Double.parseDouble(interfaceAndData[1]);
                        calculationService.reportProcessing(boxNum, interfaceAndData[0], data, null, null, createTime);
                    }
                } else {
                    //不做处理
                }
            }

            for (Map.Entry<Integer, Double> entry : digital.entrySet()) {
                String boxInterface = "K" + String.format("%02d", entry.getKey());
                if (interfaces.contains("," + boxInterface + ",")) {
                    String closeTime = digitalClose.get(entry.getKey());
                    String breakTime = digitalBreak.get(entry.getKey());
                    if (StringUtils.isEmpty(closeTime)) {
                        closeTime = "0";
                    }
                    if (StringUtils.isEmpty(breakTime)) {
                        breakTime = "0";
                    }
                    calculationService.reportProcessing(boxNum, boxInterface, entry.getValue(), closeTime, breakTime, createTime);
                }
            }

        }
    }

    /**
     * @param ctx
     * @param realData 报文数据
     * @param boxNum   盒子的IMEI
     */
    private void dataProcessingType4(ChannelHandlerContext ctx, String realData, String boxNum, String interfaces) {
        if (boxNum != null) {
            Map<Integer, Double> digital = new HashMap<Integer, Double>();
            Map<Integer, String> digitalClose = new HashMap<Integer, String>();
            Map<Integer, String> digitalBreak = new HashMap<Integer, String>();
            Date currentDate = new Date();
            String createTime = DateToString(currentDate, null);

            //realData P001:00000;P002:00000;P003:00000;P004:00000;P005:00000;P006:00000;P007:00000;P008:00000;P009:00000;P010:00000;
            //         P201:00000;P202:00000;P203:00000;P204:00000;P205:00000;P206:00000;P207:00000;P208:00000;P209:00000;P210:00000;
            // K01:00000101010000000000;

            String[] datas = realData.split(";");
            for (String dataOne : datas) {
                String[] interfaceAndData = dataOne.split(":");
                if (interfaceAndData.length != 2) {
                    break;
                }
                //如果是P001-P128 代表 数字量闭合时长
                //更改：分组上报不一定从P001 开始可能P017到P032
                if ("P0".equals(interfaceAndData[0].substring(0, 2)) || "P1".equals(interfaceAndData[0].substring(0, 2))) {
                    int i = Integer.parseInt(interfaceAndData[0].substring(1).replaceAll("^(0+)", ""));
                    String countTime = getCountTime(interfaceAndData);
                    digitalClose.put(i, countTime);
                }
                //如果是P201-P328 代表 数字量断开时长
                //更改：分组上报不一定从P001 开始可能P217到P232
                else if ("P2".equals(interfaceAndData[0].substring(0, 2)) || "P3".equals(interfaceAndData[0].substring(0, 2))) {
                    int i = Integer.parseInt(interfaceAndData[0].substring(1).replaceAll("^(0+)", ""));
                    String countTime = getCountTime(interfaceAndData);
                    digitalBreak.put(i - 200, countTime);
                }
                //如果是K01 代表 开关量状态 从第十一个数开始要
                else if ("K01".equals(interfaceAndData[0])) {
                    //K01:00000101010000000000
                    //k01:0000000000010011111011010011101010
                    //k01:0000000000010011111011010011101010
                    //k01:0000000000000000000111010000100010
                    String digitalAll = interfaceAndData[1];
                    for (int i = 0; i < digitalAll.length(); i++) {
                        String value = digitalAll.substring(i, i + 1);
                        digital.put(i + 1, Double.parseDouble(value));
                    }
                } else {
                    //不做处理。。
                }
            }
            for (Map.Entry<Integer, String> entry : digitalClose.entrySet()) {
                String boxInterface = "K" + (entry.getKey() + 10);
                if (interfaces.contains("," + boxInterface + ",")) {
                    String data1 = null;
                    String index = null;
                    if (entry.getKey() < 10) {
                        index = "1" + entry.getKey();
                    } else if (10 <= entry.getKey() && entry.getKey() < 50) {
                        int i1 = entry.getKey() + 10;
                        index = String.valueOf(i1);
                    }

                    Double data = digital.get(Integer.parseInt(index));
                    //                    System.out.println(data);
                    String breakTime = digitalBreak.get(entry.getKey());
                    //                    System.out.println(breakTime);
                    //                    System.out.println(entry.getValue());
                    //                    if (StringUtils.isEmpty(breakTime)) {
                    //                        breakTime = "0";
                    //                    }
                    //                    if (StringUtils.isEmpty(data)) {
                    //                        data = 0.0;
                    //                    }
                    if (1 == data || 1.0 == data) {
                        data1 = "开启";
                    }
                    if (0 == data || 0.0 == data) {
                        data1 = "关闭";
                    }
                    calculationService.reportProcessing(boxNum, boxInterface, data1, entry.getValue(), breakTime, createTime);
                }
                //            for (Map.Entry<Integer, Double> entry : digital.entrySet()) {
                //                String data = null;
                //
                //                String boxInterface = "K" + entry.getKey();
                //                if (interfaces.contains("," + boxInterface + ",")) {
                //                    String closeTime = digitalClose.get(entry.getKey());
                //                    String breakTime = digitalBreak.get(entry.getKey());
                //                    if (StringUtils.isEmpty(closeTime)) {
                //                        closeTime = "0";
                //                    }
                //                    if (StringUtils.isEmpty(breakTime)) {
                //                        breakTime = "0";
                //                    }
                //                    Double value = entry.getValue();
                //                    if(1==value||1.0==value){
                //                        data="开启";
                //                    }
                //                    if(0==value||0.0==value){
                //                        data="关闭";
                //                    }
                //                    calculationService.reportProcessing(boxNum, boxInterface, data, closeTime, breakTime, createTime);
                //                }
            }

        }
    }

    /**
     * @param ctx
     * @param realData 报文数据
     * @param boxNum   盒子的IMEI
     */
    private void dataProcessingType5(ChannelHandlerContext ctx, String realData, String boxNum, String interfaces) {
        if (boxNum != null) {
            Date currentDate = new Date();
            String createTime = DateToString(currentDate, null);
            //   System.out.println(createTime);

            //realData A01:00000;A02:00000;A03:00000;A04:00000;A05:00000;A06:00000;A07:00000;A08:00000;A09:00000;A10:00000;

            String[] datas = realData.split(";");
            for (String dataOne : datas) {
                String[] interfaceAndData = dataOne.split(":");
                if (interfaceAndData.length != 2) {
                    break;
                }
                if ("A".equals(interfaceAndData[0].substring(0, 1))) {
                    if (interfaces.contains("," + interfaceAndData[0] + ",")) {

                        Double data = Double.parseDouble(interfaceAndData[1]);
                        if (data >= 32768) {
                            data = data - 65536;
                        }
                        calculationService.reportProcessing(boxNum, interfaceAndData[0], data, null, null, createTime);
                    }
                } else {
                    //不做处理
                }
            }

        }
    }

    private String getBoxNum(String imeiOrBoxNum) {
        String sql = "SELECT box_num FROM box WHERE box_num='" + imeiOrBoxNum + "' OR imei='" + imeiOrBoxNum + "'";
        String boxNum = regionMapper.selectStrSQL(sql);
        return boxNum;
    }

    private ByteBuf getByteBuf(ChannelHandlerContext ctx) {
        byte[] bytes = "@888".getBytes(Charset.forName("utf-8"));
        ByteBuf buffer = ctx.alloc().buffer();
        buffer.writeBytes(bytes);
        return buffer;
    }

    public static void main(String[] args) {
        String example = "008952510";
        BigDecimal divide1 = new BigDecimal(example.substring(0, 5)).divide(new BigDecimal(5));
        String result = Convert.toStr(divide1.multiply(new BigDecimal(10000)).add(new BigDecimal(example.substring(5))).setScale(0));
        System.out.println(result);
//        String a = "000070011";
//        BigDecimal divide = new BigDecimal(a.substring(0, 5)).divide(new BigDecimal(5));
//        String result = Convert.toStr(divide.multiply(new BigDecimal(10000)).add(new BigDecimal(a.substring(5))).setScale(0));
//        System.out.println(result);
    }

    //007550095
    //前五位/5*10000+后四位
    //后四位满10000往前五位各位上+5
    private String getCountTime(String[] ctx) {
        BigDecimal a1 = new BigDecimal(ctx[1].substring(0, 5));
        BigDecimal a2 = new BigDecimal(5);
        BigDecimal divide = a1.divide(a2);
        BigDecimal a3 = new BigDecimal(10000);
        BigDecimal multiply = divide.multiply(a3);
        BigDecimal a4 = new BigDecimal(ctx[1].substring(5));
        BigDecimal result = multiply.add(a4).setScale(0);
        String s = String.valueOf(result);
        return s;
    }

}
